package com.shop.service.impl;

import com.github.pagehelper.PageInfo;
import com.shop.entity.Cart;
import com.shop.mapper.CartMapper;
import com.shop.service.CartService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("cartService")
@RequiredArgsConstructor
@Transactional(rollbackFor = RuntimeException.class,
        propagation = Propagation.REQUIRED)
public class CartServiceImpl implements CartService {

    private final CartMapper cartMapper;

    @Override
    public PageInfo<Cart> listCartMappings(int user_id) {
        return new PageInfo<>(cartMapper.listCartMappings(user_id));
    }

    @Override
    public List<Cart> listCart(Integer pageNum, Integer pageSize, Integer userId) {
        List<Cart> carts = cartMapper.listCart(pageNum, pageSize, userId);
        return new ArrayList<>(carts);
    }

    @Override
    public List<Cart> listCartMappings(Integer id) {
        List<Cart> list =  cartMapper.listCartMappings(id);
        return new ArrayList<>(list);
    }

    /**
     * 添加购物车
     * @param cart
     */
    @Override
    public void addCart(Cart cart) {
        cartMapper.addCart(cart);
    }

    /**
     * 更新购物车数量
     * @param id
     * @param quantity
     * @return
     */
    @Override
    public int updateCartNum(Integer id, int quantity) {
        int count = cartMapper.updateCartNum(id, quantity);
        return count;
    }

    /**
     * 删除购物车
     * @param userId
     * @param productIds
     */
    @Override
    public void deleteCartBatch(Integer userId, List<Integer> productIds) {
        cartMapper.deleteCartBatch(userId, productIds);
    }

    @Override
    public void deleteCart(Integer id) {
        cartMapper.deleteCart(id);
    }


}
